package ua.com.infinite.mobile.utils;

import org.json.JSONException;
import org.json.JSONObject;

import ua.com.infinite.mobile.plains.User;
import ua.com.infinite.mobile.server.Constants;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class SharedPreferencesUtils {

	public final static String APP_SETTINGS = "app_settings";
	public final static String USER_SETTINGS = "user_settings";
	
	
	public static boolean isLogin(Context context){
		String str = context.getSharedPreferences(USER_SETTINGS, 0).getString(User.IS_LOGIN, "false"); 
		return new Boolean(str.trim());
	}
	
	public static void login(Context context, JSONObject object){
		try {
			setPreference(context, true, User.USER_ID, object.getString(User.USER_ID));
			setPreference(context, true, User.IS_LOGIN, object.getString(User.IS_LOGIN));
		} catch (JSONException e) {
			
			e.printStackTrace();
		}
		
	}
	
	public static void logoff(Context context){
			setPreference(context, true, User.IS_LOGIN, "false");
			setPreference(context, true, User.USER_ID, "");
	}
	
	public static void  setPreference(Context context, boolean userSettings, String key, String value){
		String prefName = userSettings?USER_SETTINGS:APP_SETTINGS;
		SharedPreferences prefs = context.getSharedPreferences(prefName, 2);
		Editor editor = prefs.edit();
		editor.putString(key, value);
		editor.commit();
	}
	
	public static String getPreference(Context context, boolean userSettings, String key){
		String prefName = userSettings?USER_SETTINGS:APP_SETTINGS;
		return context.getSharedPreferences(prefName, 0).getString(key, "");
	}
	
}
